package com.sgu6.sgu6_machine.network;


import com.sgu6.sgu6_machine.common.Constant;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetWork {

    public static NetWork instance;

    public static NetWork getInstance() {
        if (instance == null) {
            instance = new NetWork();
        }
        return instance;
    }

    public static ApiService getApiService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MachineContainer.machineConfigDto.isBeta ? MachineContainer.machineConfigDto.apiDomain : Constant.API_DOMAIN_DEV)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient().build())
                .build();
//        ApiService apiService = retrofit.create(ApiService.class);
        return retrofit.create(ApiService.class);
    }

    public static AdminApiService getAdminApiService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MachineContainer.machineConfigDto.isBeta ? MachineContainer.machineConfigDto.adminApiDomain : Constant.ADMIN_API_DOMAIN_DEV)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient().build())
                .build();
//        ApiService apiService = retrofit.create(ApiService.class);

        return retrofit.create(AdminApiService.class);
    }


    private static OkHttpClient.Builder httpClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        // add your other interceptors …

        // add logging as last interceptor
        httpClient.addInterceptor(logging);  // <-- this is the important line!

        return httpClient;
    }

}
